﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;
using TugasAkhir.Objects;

namespace TugasAkhir
{
    public partial class DaftarRekomendasi : System.Web.UI.Page
    {
        private DataSet hasilQuery;
        private DosenObject dosen;


        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            CheckRole();
            if (!Page.IsCallback)
            {
                if (IsSearchMode())
                {
                    this.InitTabelRekomendasi(Request.QueryString["q"]);
                    this.ltKataKunci.Text = Request.QueryString["q"];
                    this.searchLabel.Attributes["class"] = "col-sm-6";
                    this.searchBoxSm.Attributes["class"] = "col-sm-6";
                }
                else
                {
                    this.InitTabelRekomendasi();
                }
            }
        }

        private void InitTabelRekomendasi()
        {
            if (RekomendasiModel.GetTabelRekomendasi(out this.hasilQuery))
            {
                this.gvRekomendasi.DataSource = this.hasilQuery.Tables[0];
                this.gvRekomendasi.DataBind();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", "Init();", true);
            }
        }

        private void InitTabelRekomendasi(string kataKunci)
        {
            if (RekomendasiModel.GetTabelRekomendasi(kataKunci, out this.hasilQuery))
            {
                this.gvRekomendasi.DataSource = this.hasilQuery.Tables[0];
                this.gvRekomendasi.DataBind();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", "Init();", true);
            }
        }

        private void CheckRole()
        {
            if (int.Parse(Session["id_role"].ToString().Substring(2)) == 2)
            {
                this.liPertanyaan.Attributes.Remove("hidden");
                this.liPengaturanPertanyaan.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else if (int.Parse(Session["id_role"].ToString().Substring(2)) == 3)
            {
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else
            {
                this.liRekomendasi.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.liDaftarRekomendasi.Attributes.Remove("hidden");
            }
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"].ToString() != "" && int.Parse(Session["id_role"].ToString().Substring(2)) >= 4)
            {
                DosenModel.GetDosenDetail(Session["username"].ToString(), out this.dosen);
                this.litName.Text = dosen.NamaDosen;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private bool IsSearchMode()
        {
            return Request.QueryString["q"] != null && Request.QueryString["q"] != string.Empty;
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }

        protected void gvRekomendasi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvRekomendasi.PageIndex = e.NewPageIndex;
            if (IsSearchMode())
            {
                this.InitTabelRekomendasi(Request.QueryString["q"]);
            }
            else
            {
                this.InitTabelRekomendasi();
            }

            //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "InitScript", "Init();", true);
        }

        protected void gvRekomendasi_DataBound(object sender, EventArgs e)
        {
            for (int rowIndex = gvRekomendasi.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow gvRow = gvRekomendasi.Rows[rowIndex];
                GridViewRow gvPreviousRow = gvRekomendasi.Rows[rowIndex + 1];

                if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
                {
                    //gvPreviousRow.Cells[0].Text = "";
                    if (gvPreviousRow.Cells[0].RowSpan < 2)
                    {
                        gvRow.Cells[1].RowSpan = 2;
                        //gvRow.Cells[2].RowSpan = 2;
                        gvRow.Cells[0].RowSpan = 2;
                    }
                    else
                    {
                        gvRow.Cells[1].RowSpan = gvPreviousRow.Cells[1].RowSpan + 1;
                        //gvRow.Cells[2].RowSpan = gvPreviousRow.Cells[2].RowSpan + 1;
                        gvRow.Cells[0].RowSpan = gvPreviousRow.Cells[0].RowSpan + 1;
                    }
                    gvPreviousRow.Cells[1].Visible = false;
                    //gvPreviousRow.Cells[2].Visible = false;
                    gvPreviousRow.Cells[0].Visible = false;
                    //hidden++;
                }
            }
        }

        protected void gvRekomendasi_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("addRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.AddCommand(index);
            }
            else if (e.CommandName.Equals("updateRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.UpdateCommand(index);
            }
            else if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        private void AddCommand(int index)
        {
            string idPertanyaan = this.hasilQuery.Tables[0].Rows[index][1].ToString();
            this.hfAdd.Value = idPertanyaan;

            var pertanyaan = new PertanyaanObject();
            PertanyaanModel.GetDetailPertanyaan(this.hfAdd.Value, out pertanyaan);
            this.lblPertanyaan.Text = pertanyaan.Pertanyaan;

            this.txtAddRekomendasi.Text = string.Empty;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowAdd", "$('#addModal').modal('show');", true);
        }

        private void UpdateCommand(int index)
        {
            string idPertanyaan = this.hasilQuery.Tables[0].Rows[index][1].ToString();
            this.hfEditPertanyaan.Value = idPertanyaan;
            var pertanyaan = new PertanyaanObject();
            PertanyaanModel.GetDetailPertanyaan(idPertanyaan, out pertanyaan);

            this.lblPertanyaanEdit.Text = pertanyaan.Pertanyaan;

            string idRekomendasi = this.hasilQuery.Tables[0].Rows[index][0].ToString();
            this.hfEdit.Value = idRekomendasi;

            var rekomendasi = new RekomendasiObject();
            RekomendasiModel.GetDetailRekomendasi(this.hfEdit.Value, out rekomendasi);

            this.txtEditRekomendasi.Text = rekomendasi.Rekomendasi;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowEdit", "$('#editModal').modal('show');", true);
        }

        private void DeleteCommand(int index)
        {
            string idRekomendasi = this.hasilQuery.Tables[0].Rows[index][0].ToString();

            this.hfDeleteID.Value = idRekomendasi;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDelete", "$('#deleteModal').modal('show');", true);
        }

        protected void btAddRekomendasi_Click(object sender, EventArgs e)
        {
            string id;
            RekomendasiModel.GetId(out id);

            var rekomendasi = new RekomendasiObject();
            rekomendasi.IdRekomendasi = id;
            rekomendasi.IdPertanyaan = this.hfAdd.Value;
            rekomendasi.Rekomendasi = this.txtAddRekomendasi.Text;

            if (RekomendasiModel.TambahRekomendasi(rekomendasi))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAdd", "$('#addModal').modal('hide');", true);

                if (IsSearchMode())
                {
                    this.InitTabelRekomendasi(Request.QueryString["q"]);
                }
                else
                {
                    this.InitTabelRekomendasi();
                }
            }
        }

        protected void btDeleteRekomendasi_Click(object sender, EventArgs e)
        {
            if (RekomendasiModel.HapusRekomendasi(this.hfDeleteID.Value))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideDelete", "$('#deleteModal').modal('hide');", true);

                if (IsSearchMode())
                {
                    this.InitTabelRekomendasi(Request.QueryString["q"]);
                }
                else
                {
                    this.InitTabelRekomendasi();
                }
            }
        }

        protected void btEditRekomendasi_Click(object sender, EventArgs e)
        {
            var rekomendasi = new RekomendasiObject();
            rekomendasi.IdRekomendasi = this.hfEdit.Value;
            rekomendasi.IdPertanyaan = this.hfEditPertanyaan.Value;
            rekomendasi.Rekomendasi = this.txtEditRekomendasi.Text;

            if (RekomendasiModel.UbahRekomendasi(rekomendasi))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideEdit", "$('#editModal').modal('hide');", true);

                if (IsSearchMode())
                {
                    this.InitTabelRekomendasi(Request.QueryString["q"]);
                }
                else
                {
                    this.InitTabelRekomendasi();
                }
            }
        }

        protected void btCari_ServerClick(object sender, EventArgs e)
        {
            if (this.tbCariSm.Text != string.Empty || this.tbCariXs.Text != string.Empty)
            {
                Response.Redirect("DaftarRekomendasi.aspx?q=" + this.tbCariSm.Text, true);
            }
        }
    }
}